﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class ListPatient : UserControl
    {
        public ListPatient()
        {
            InitializeComponent();
        }
        private string form;
        private int option;

        public ListPatient(int option)
        {
            InitializeComponent();
            this.option = option;
            btn_Search.Enabled = false;
            tbx_ContentSearch.ReadOnly = true;
            cbx_TypeSearch.Enabled = false;
            btn_Edit.Enabled = false;
            btn_Payment.Enabled = false;
            btn_History.Enabled = false;
            btn_PrintPatient.Enabled = false;
            form = "Edit Patient";
            switch(option)
            {
                case 1:
                    btn_Edit.Enabled = true;
                    form = "Edit Patient";
                    break;
                case 2:
                    btn_Search.Enabled = true;
                    tbx_ContentSearch.ReadOnly = false;
                    cbx_TypeSearch.Enabled = true;
                    btn_History.Enabled = true;
                    btn_Payment.Enabled = true;
                    form = "Search Patient";
                    btn_PrintPatient.Enabled = true;
                    break;
                case 3:
                    btn_Search.Enabled = true;
                    tbx_ContentSearch.ReadOnly = false;
                    cbx_TypeSearch.Enabled = true;
                    btn_History.Enabled = true;
                    form = "History Patient";
                    break;
                case 4:
                    btn_Search.Enabled = true;
                    tbx_ContentSearch.ReadOnly = false;
                    cbx_TypeSearch.Enabled = true;
                    btn_Payment.Enabled = true;
                    form = "Payment Patient";
                    break;
                default:
                    break;
            }
        }

        private void ObjListPatient(int value,int to)
        {
            lv_Patient.Items.Clear();
            List<Obj_Patient> list = BL_Patients.ListPatient(value,to);
            Obj_OrganizationDetails org = new Obj_OrganizationDetails();
            for (int i = 0; i < list.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(list[i].PatientID.ToString());
                lvi.SubItems.Add(list[i].HospitalizedDate.ToString());
                org.OrganizationDetailsID = list[i].OrganizationDetailsID;
                lvi.SubItems.Add(BL_OrganizationDetailss.ObjOrganizationDetails(org).NameDetails.ToString());
                lvi.SubItems.Add(list[i].States.ToString());
                lvi.SubItems.Add(list[i].Gender.ToString());
                lvi.SubItems.Add(list[i].Name.ToString());
                lvi.SubItems.Add(list[i].BirthDay.ToString());
                lvi.SubItems.Add(list[i].TypePatient.ToString());
                lvi.SubItems.Add(list[i].Cardnumber.ToString());
                lvi.SubItems.Add(list[i].TypeTreatment.ToString());
                lvi.SubItems.Add(list[i].HouseID.ToString() + " " + list[i].Stress.ToString() + " " + list[i].Ward.ToString() + " " + list[i].District.ToString() + " " + list[i].City.ToString());
                lvi.SubItems.Add(list[i].PhoneNumber.ToString());
                lvi.SubItems.Add(list[i].Ethnic.ToString());
                lvi.SubItems.Add(list[i].AddressEmail.ToString());
                lvi.SubItems.Add(list[i].Occupation.ToString());
                lvi.SubItems.Add(list[i].ContactPerson.ToString());
                lv_Patient.Items.Add(lvi);
            }
        }

        private void ListPatient_Load(object sender, EventArgs e)
        {
            ObjListPatient(0,30);
            cbx_ShowRow.SelectedIndex = 0;
            cbx_TypeSearch.SelectedIndex = 0;
            lbl_TotalValue.Text = BL_Patients.ListPatient(0,30).Count.ToString();
        }

        private void lv_Patient_Click(object sender, EventArgs e)
        {
            if (this.option == 1)
            {
                Obj_Patient patient = new Obj_Patient();
                patient.PatientID = Convert.ToInt32(lv_Patient.SelectedItems[0].Text.ToString());
                ((Managements)this.Parent.Parent.Parent).EditPatient(patient.PatientID);
            }
            else if (this.option == 2)
            { }
            else if (this.option == 3)
            {
                HistoryOrPayment(this.option);
            }
            else if (this.option == 4)
            {
                HistoryOrPayment(this.option);
            }
        }

        private void HistoryOrPayment( int type)
        {
            Obj_Patient patient = new Obj_Patient();
            patient.PatientID = Convert.ToInt32(lv_Patient.SelectedItems[0].Text.ToString());
            ((Managements)this.Parent.Parent.Parent).ShowHistoryOrPayment(type, patient);
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            Obj_Patient patient = new Obj_Patient();
            if (lv_Patient.SelectedItems.Count == 0)
                LibraryLanguages.ErrorSelect("Edit Patient");
            else
            {
                patient.PatientID = Convert.ToInt32(lv_Patient.SelectedItems[0].Text.ToString());
                ((Managements)this.Parent.Parent.Parent).EditPatient(patient.PatientID);
            }
        }

        private void cbx_ShowRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowListPatient();
        }

        private void ShowListPatient()
        {
            if (cbx_ShowRow.SelectedIndex == 0)
                ObjListPatient(0, 30);
            else if (cbx_ShowRow.SelectedIndex == 1)
                ObjListPatient(30, 60);
            else if (cbx_ShowRow.SelectedIndex == 2)
                ObjListPatient(60, 90);
            else if (cbx_ShowRow.SelectedIndex == 3)
                ObjListPatient(90, 120);
            else if (cbx_ShowRow.SelectedIndex == 4)
                ObjListPatient(120, 150);
            else if (cbx_ShowRow.SelectedIndex == 5)
                ObjListPatient(150, 180);
            else if (cbx_ShowRow.SelectedIndex == 6)
                ObjListPatient(180, 210);
            else if (cbx_ShowRow.SelectedIndex == 7)
                ObjListPatient(210, 240);
            else if (cbx_ShowRow.SelectedIndex == 8)
                ObjListPatient(240, 270);
            else if (cbx_ShowRow.SelectedIndex == 9)
                ObjListPatient(270, 300);
            else
                ObjListPatient(0, 1000);
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            SearchPatient();
        }

        private void SearchPatient()
        {
            if (string.IsNullOrEmpty(tbx_ContentSearch.Text) == true)
                LibraryLanguages.ContentSeartIsNullOrEmpty();
            else
            {
                if (cbx_TypeSearch.SelectedIndex == 0)
                {
                    Obj_Patient obj = new Obj_Patient();
                    if (BL_Patients.CheckPatient(tbx_ContentSearch.Text) == false)
                        LibraryLanguages.ErrorContentSearch(tbx_ContentSearch.Text + " can't not format is number !");
                    else
                    {
                        obj.PatientID = Convert.ToInt32(tbx_ContentSearch.Text.ToString());
                        Patient(0, obj);
                    }
                }
                else if (cbx_TypeSearch.SelectedIndex == 1)
                {
                    Obj_Patient obj = new Obj_Patient();
                    obj.Cardnumber = tbx_ContentSearch.Text.ToString();
                    Patient(1, obj);
                }
            }
        }

        private void Patient(int p, Obj_Patient obj)
        {
            Obj_Patient patient = BL_Patients.SearchPatient(p,obj);
            if (patient == null)
                LibraryLanguages.NoRowSelect("No find content where " + cbx_TypeSearch.Text + " as : " + tbx_ContentSearch.Text);
            else
            {
                lv_Patient.Items.Clear();
                ListViewItem lvi = new ListViewItem(patient.PatientID.ToString());
                lvi.SubItems.Add(patient.HospitalizedDate.ToString());
                Obj_OrganizationDetails org = new Obj_OrganizationDetails();
                org.OrganizationDetailsID = patient.OrganizationDetailsID;
                lvi.SubItems.Add(BL_OrganizationDetailss.ObjOrganizationDetails(org).NameDetails.ToString());
                lvi.SubItems.Add(patient.States.ToString());
                lvi.SubItems.Add(patient.Gender.ToString());
                lvi.SubItems.Add(patient.Name.ToString());
                lvi.SubItems.Add(patient.BirthDay.ToString());
                lvi.SubItems.Add(patient.TypePatient.ToString());
                lvi.SubItems.Add(patient.Cardnumber.ToString());
                lvi.SubItems.Add(patient.TypeTreatment.ToString());
                lvi.SubItems.Add(patient.HouseID.ToString() + " " + patient.Stress.ToString() + " " + patient.Ward.ToString() + " " + patient.District.ToString() + " " + patient.City.ToString());
                lvi.SubItems.Add(patient.PhoneNumber.ToString());
                lvi.SubItems.Add(patient.Ethnic.ToString());
                lvi.SubItems.Add(patient.AddressEmail.ToString());
                lvi.SubItems.Add(patient.Occupation.ToString());
                lvi.SubItems.Add(patient.ContactPerson.ToString());
                lv_Patient.Items.Add(lvi);
            }
        }

        private void tbx_ContentSearch_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SearchPatient();
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            if (LibraryLanguages.CancelUserControl(form) == true)
                ((Managements)this.Parent.Parent.Parent).ShowAddPatient(8);
        }

        private void btn_History_Click(object sender, EventArgs e)
        {
            if (lv_Patient.SelectedItems.Count == 0)
                LibraryLanguages.ErrorSelect("view history");
            else
            {
                Obj_Patient patient = new Obj_Patient();
                patient.PatientID = Convert.ToInt32(lv_Patient.SelectedItems[0].Text.ToString());
                ((Managements)this.Parent.Parent.Parent).ShowHistoryOrPayment(3, patient);
            }
        }

        private void btn_Payment_Click(object sender, EventArgs e)
        {
            if (lv_Patient.SelectedItems.Count == 0)
                LibraryLanguages.ErrorSelect("view payment");
            else
            {
                HistoryOrPayment(this.option);
            }
        }

        private void btn_PrintPatient_Click(object sender, EventArgs e)
        {
            List<Obj_Patient> list;
            if (cbx_ShowRow.SelectedIndex == 0)
                list = BL_Patients.ListPatient(0, 30);
            else if (cbx_ShowRow.SelectedIndex == 1)
                list = BL_Patients.ListPatient(30, 60);
            else if (cbx_ShowRow.SelectedIndex == 2)
                list = BL_Patients.ListPatient(60, 90);
            else if (cbx_ShowRow.SelectedIndex == 3)
                list = BL_Patients.ListPatient(90, 120);
            else if (cbx_ShowRow.SelectedIndex == 4)
                list = BL_Patients.ListPatient(120, 150);
            else if (cbx_ShowRow.SelectedIndex == 5)
                list = BL_Patients.ListPatient(150, 180);
            else if (cbx_ShowRow.SelectedIndex == 6)
                list = BL_Patients.ListPatient(180, 210);
            else if (cbx_ShowRow.SelectedIndex == 7)
                list = BL_Patients.ListPatient(210, 240);
            else if (cbx_ShowRow.SelectedIndex == 8)
                list = BL_Patients.ListPatient(240, 270);
            else if (cbx_ShowRow.SelectedIndex == 9)
                list = BL_Patients.ListPatient(270, 300);
            else
                list = BL_Patients.ListPatient(0, 1000);

            List<Obj_OrganizationDetails> ListOrg = new List<Obj_OrganizationDetails>();
            for (int i = 0; i < list.Count; i++)
            {
                Obj_OrganizationDetails obj = new Obj_OrganizationDetails();
                obj.OrganizationDetailsID = list[i].OrganizationDetailsID;
                BL_OrganizationDetailss.ObjOrganizationDetails(obj).NameDetails.ToString();
                ListOrg.Add(obj);
            }
            PrintListPatient plp = new PrintListPatient(list,ListOrg);
            plp.ShowDialog();
        }
    }
}
